package org.aplombh.java.interview.newcoder;

import java.util.*;

//HJ77 火车进站
public class HJ77_火车进站 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] seq = new int[n];
        for (int i = 0; i < n; i++) {
            seq[i] = in.nextInt();
        }
        allMethods(seq, 0, 0, "");
        Collections.sort(path);
        for (String s : path) {
            System.out.println(s);
        }
    }

    static Stack<Integer> stack = new Stack<>();
    static List<String> path = new ArrayList<>();

    public static void allMethods(int[] seq, int number, int pushedNumber, String res) {

        // 记录结果，全部出站
        if (number == seq.length) {
            path.add(res);
        }

        // 将火车出站,并记录出站顺序
        if (!stack.empty()) {
            int pop = stack.pop();  // 非空先出战
            allMethods(seq, number + 1, pushedNumber, res + pop + " ");
            stack.push(pop);    // 恢复现场
        }

        // 火车未全部进站
        if (pushedNumber < seq.length) {
            stack.push(seq[pushedNumber]);  // 未出站数小于总数，说明未进站完全，先进站
            allMethods(seq, number, pushedNumber + 1, res);
            stack.pop();    // 恢复现场
        }
    }
}